﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UserControl = System.Windows.Controls.UserControl;

namespace PlaceOrder_Client.Views
{
    /// <summary>
    /// OrderListView.xaml 的交互逻辑
    /// </summary>
    public partial class OrderListView : UserControl
    {
        public OrderListView()
        {
            InitializeComponent();
        }

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {

        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {

        }
   
        // 泛型方法：查找视觉树中的父元素
        private T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
        {
            while (child != null)
            {
                if (child is T parent)
                {
                    return parent;
                }

                child = VisualTreeHelper.GetParent(child);
            }

            return null;
        }

   

        private void DataGridRow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is DataGridRow row)
            {
                var dataGrid = FindVisualParent<DataGrid>(row);
                if (dataGrid != null)
                {
                    var dataContext = row.DataContext;

                    if (dataGrid.SelectedItem == dataContext)
                    {
                        dataGrid.SelectedItem = null;  // 取消选中
                    }
                    else
                    {
                        dataGrid.SelectedItem = dataContext;  // 选中点击的行
                    }
                }
            }
        }
    }
}
